package team.yidong.fenda.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import team.yidong.fenda.entity.Question;
import team.yidong.fenda.entity.User;

import java.util.List;

/**
 * Created by xuanlong on 2017/10/12.
 */
@Repository
public interface QuestionRepository extends JpaRepository<Question, Integer>, JpaSpecificationExecutor<Question> {
    @Query("select question from Question question join question.user user join user.hospital hospital where hospital.id=?1 and question.isSolved='是' and question.status='white' and question.isOneMaster=0")
    List<Question> findByHospital(Integer id, Pageable pageable);

    @Query("select question from Question question join question.user user join user.hospital hospital where hospital.id=?1 and question.questionClass=?2 and question.status='white' and question.isSolved<>'否' and question.isOneMaster=0")
    List<Question> findByQuestionClass(Integer id, String questionClass, Pageable pageable);

    @Query("select question from Question question join question.user user where user.userOpenId=?1 and question.status='white' order by question.creatDate DESC")
    List<Question> findByUser(String wxNumber);

    Question findByUuName(String uuName);

    List<Question> findByIsOneMaster(Integer masterId);

    @Query("select question from Question question join question.user user join  user.hospital hospital where hospital.id=?1 and question.status='white'")
    List<Question> findLatestQuestionByHospital(Integer hospitalId, Sort sort);

    @Query("select distinct question from Question question join question.answerList answer join answer.master master where master.openId=?1 and question.status='white'  order by question.creatDate DESC ")
    List<Question> findAllQuestionByAnswerMaster(String openId);

    @Query("select distinct question from Question question join question.answerList answer join answer.master master where master.openId=?1 and question.status='white' and question.isOneMaster=0 order by question.creatDate DESC ")
    List<Question> findQuestionByAnswerMaster(String openId);

    @Query("select distinct question.questionClass from Question question join question.user user join user.hospital hospital where hospital.id=?1")
    List<String> findQuestionClassByHospital(Integer hospitalId);

    @Query("select distinct question.questionClass from Question question ")
    List<String> findAllQuestionClass();

    @Query("select distinct question from Question question join question.answerList answer join answer.master master where master.openId=?1 and question.status='white' and question.isOneMaster<>0 order by question.creatDate DESC ")
    List<Question> findTewenQuestion(String openid);

    @Modifying
    @Query("update Question question set question.questionClass=?1 where question.questionClass=?2")
    void updateQuestionClass(String questionClass1, String questionClass2);
}
